#!/bin/sh /etc/rc.common
# Copyright (C) 2019 OpenWrt.org

START=99
USE_PROCD=1

NAME=wrtbwmon
PID_FILE=/var/run/wrtbwmon.pid
args=/usr/sbin/wrtbwmon

create_instance() {
	procd_open_instance
	procd_set_param command $args
	procd_set_param respawn
	procd_set_param user root
#	procd_set_param pidfile $PID_FILE
	procd_close_instance
}

service_triggers()
{
	procd_add_reload_trigger "$NAME"
}

start_service() {
	local db enabled
	config_load $NAME

	config_get db general path
	[ -z "$dbpath" ] && dbpath="/tmp/usage.db"
	append args " -46"
	append args "-f $dbpath"
	append args "-p /tmp/usage.htm"
	append args "-u /etc/wrtbwmon.user"
	append args "-d"

	config_get enabled  general enabled
	[ "$enabled"0 -eq 0 ] || create_instance
}

stop_service() {
	procd_kill wrtbwmon
	kill -CONT $(cat $PID_FILE)
}

